home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / info / cl-1.z / cl-1
Encoding:
GNU Info File  |  1998-10-28  |  45.5 KB  |  1,044 lines

  1. This is Info file ../info/cl, produced by Makeinfo-1.64 from the input
  2. file cl.texi.
  3.  
  4.    This file documents the GNU Emacs Common Lisp emulation package.
  5.  
  6.    Copyright (C) 1993 Free Software Foundation, Inc.
  7.  
  8.    Permission is granted to make and distribute verbatim copies of this
  9. manual provided the copyright notice and this permission notice are
  10. preserved on all copies.
  11.  
  12.    Permission is granted to copy and distribute modified versions of
  13. this manual under the conditions for verbatim copying, provided also
  14. that the section entitled "GNU General Public License" is included
  15. exactly as in the original, and provided that the entire resulting
  16. derived work is distributed under the terms of a permission notice
  17. identical to this one.
  18.  
  19.    Permission is granted to copy and distribute translations of this
  20. manual into another language, under the above conditions for modified
  21. versions, except that the section entitled "GNU General Public License"
  22. may be included in a translation approved by the author instead of in
  23. the original English.
  24.  
  25. 
  26. File: cl,  Node: Top,  Next: Overview,  Up: (dir)
  27.  
  28. Common Lisp Extensions
  29. **********************
  30.  
  31. This document describes a set of Emacs Lisp facilities borrowed from
  32. Common Lisp.  All the facilities are described here in detail; for more
  33. discussion and examples, Guy L. Steele's `Common Lisp, the Language',
  34. second edition, is the definitive book on Common Lisp.  While this
  35. document does not assume any prior knowledge of Common Lisp, it does
  36. assume a basic familiarity with Emacs Lisp.
  37.  
  38. * Menu:
  39.  
  40. * Overview::             Installation, usage, etc.
  41. * Program Structure::    Arglists, `eval-when', `defalias'
  42. * Predicates::           `typep', `eql', and `equalp'
  43. * Control Structure::    `setf', `when', `do', `loop', etc.
  44. * Macros::               Destructuring, `define-compiler-macro'
  45. * Declarations::         `proclaim', `declare', etc.
  46. * Symbols::              Property lists, `gensym'
  47. * Numbers::              Predicates, functions, random numbers
  48. * Sequences::            Mapping, functions, searching, sorting
  49. * Lists::                `cadr', `sublis', `member*', `assoc*', etc.
  50. * Hash Tables::          `make-hash-table', `gethash', etc.
  51. * Structures::           `defstruct'
  52. * Assertions::           `check-type', `assert', `ignore-errors'.
  53.  
  54. * Efficiency Concerns::         Hints and techniques
  55. * Common Lisp Compatibility::   All known differences with Steele
  56. * Old CL Compatibility::        All known differences with old cl.el
  57. * Porting Common Lisp::         Hints for porting Common Lisp code
  58.  
  59. * Function Index::
  60. * Variable Index::
  61.  
  62. 
  63. File: cl,  Node: Overview,  Next: Program Structure,  Prev: Top,  Up: Top
  64.  
  65. Overview
  66. ********
  67.  
  68. Common Lisp is a huge language, and Common Lisp systems tend to be
  69. massive and extremely complex.  Emacs Lisp, by contrast, is rather
  70. minimalist in the choice of Lisp features it offers the programmer.  As
  71. Emacs Lisp programmers have grown in number, and the applications they
  72. write have grown more ambitious, it has become clear that Emacs Lisp
  73. could benefit from many of the conveniences of Common Lisp.
  74.  
  75.    The "CL" package adds a number of Common Lisp functions and control
  76. structures to Emacs Lisp.  While not a 100% complete implementation of
  77. Common Lisp, "CL" adds enough functionality to make Emacs Lisp
  78. programming significantly more convenient.
  79.  
  80.    Some Common Lisp features have been omitted from this package for
  81. various reasons:
  82.  
  83.    * Some features are too complex or bulky relative to their benefit
  84.      to Emacs Lisp programmers.  CLOS and Common Lisp streams are fine
  85.      examples of this group.
  86.  
  87.    * Other features cannot be implemented without modification to the
  88.      Emacs Lisp interpreter itself, such as multiple return values,
  89.      lexical scoping, case-insensitive symbols, and complex numbers.
  90.      The "CL" package generally makes no attempt to emulate these
  91.      features.
  92.  
  93.    * Some features conflict with existing things in Emacs Lisp.  For
  94.      example, Emacs' `assoc' function is incompatible with the Common
  95.      Lisp `assoc'.  In such cases, this package usually adds the suffix
  96.      `*' to the function name of the Common Lisp version of the
  97.      function (e.g., `assoc*').
  98.  
  99.    The package described here was written by Dave Gillespie,
  100. `daveg@synaptics.com'.  It is a total rewrite of the original 1986
  101. `cl.el' package by Cesar Quiroz.  Most features of the the Quiroz
  102. package have been retained; any incompatibilities are noted in the
  103. descriptions below.  Care has been taken in this version to ensure that
  104. each function is defined efficiently, concisely, and with minimal
  105. impact on the rest of the Emacs environment.
  106.  
  107. * Menu:
  108.  
  109. * Usage::                How to use the CL package
  110. * Organization::         The package's five component files
  111. * Installation::         Compiling and installing CL
  112. * Naming Conventions::   Notes on CL function names
  113.  
  114. 
  115. File: cl,  Node: Usage,  Next: Organization,  Prev: Overview,  Up: Overview
  116.  
  117. Usage
  118. =====
  119.  
  120. Lisp code that uses features from the "CL" package should include at
  121. the beginning:
  122.  
  123.      (require 'cl)
  124.  
  125. If you want to ensure that the new (Gillespie) version of "CL" is the
  126. one that is present, add an additional `(require 'cl-19)' call:
  127.  
  128.      (require 'cl)
  129.      (require 'cl-19)
  130.  
  131. The second call will fail (with "`cl-19.el' not found") if the old
  132. `cl.el' package was in use.
  133.  
  134.    It is safe to arrange to load "CL" at all times, e.g., in your
  135. `.emacs' file.  But it's a good idea, for portability, to `(require
  136. 'cl)' in your code even if you do this.
  137.  
  138. 
  139. File: cl,  Node: Organization,  Next: Installation,  Prev: Usage,  Up: Overview
  140.  
  141. Organization
  142. ============
  143.  
  144. The Common Lisp package is organized into four files:
  145.  
  146. `cl.el'
  147.      This is the "main" file, which contains basic functions and
  148.      information about the package.  This file is relatively
  149.      compact--about 700 lines.
  150.  
  151. `cl-extra.el'
  152.      This file contains the larger, more complex or unusual functions.
  153.      It is kept separate so that packages which only want to use Common
  154.      Lisp fundamentals like the `cadr' function won't need to pay the
  155.      overhead of loading the more advanced functions.
  156.  
  157. `cl-seq.el'
  158.      This file contains most of the advanced functions for operating on
  159.      sequences or lists, such as `delete-if' and `assoc*'.
  160.  
  161. `cl-macs.el'
  162.      This file contains the features of the packages which are macros
  163.      instead of functions.  Macros expand when the caller is compiled,
  164.      not when it is run, so the macros generally only need to be
  165.      present when the byte-compiler is running (or when the macros are
  166.      used in uncompiled code such as a `.emacs' file).  Most of the
  167.      macros of this package are isolated in `cl-macs.el' so that they
  168.      won't take up memory unless you are compiling.
  169.  
  170.    The file `cl.el' includes all necessary `autoload' commands for the
  171. functions and macros in the other three files.  All you have to do is
  172. `(require 'cl)', and `cl.el' will take care of pulling in the other
  173. files when they are needed.
  174.  
  175.    There is another file, `cl-compat.el', which defines some routines
  176. from the older `cl.el' package that are no longer present in the new
  177. package.  This includes internal routines like `setelt' and
  178. `zip-lists', deprecated features like `defkeyword', and an emulation of
  179. the old-style multiple-values feature.  *Note Old CL Compatibility::.
  180.  
  181. 
  182. File: cl,  Node: Installation,  Next: Naming Conventions,  Prev: Organization,  Up: Overview
  183.  
  184. Installation
  185. ============
  186.  
  187. Installation of the "CL" package is simple:  Just put the byte-compiled
  188. files `cl.elc', `cl-extra.elc', `cl-seq.elc', `cl-macs.elc', and
  189. `cl-compat.elc' into a directory on your `load-path'.
  190.  
  191.    There are no special requirements to compile this package: The files
  192. do not have to be loaded before they are compiled, nor do they need to
  193. be compiled in any particular order.
  194.  
  195.    You may choose to put the files into your main `lisp/' directory,
  196. replacing the original `cl.el' file there.  Or, you could put them into
  197. a directory that comes before `lisp/' on your `load-path' so that the
  198. old `cl.el' is effectively hidden.
  199.  
  200.    Also, format the `cl.texinfo' file and put the resulting Info files
  201. in the `info/' directory or another suitable place.
  202.  
  203.    You may instead wish to leave this package's components all in their
  204. own directory, and then add this directory to your `load-path' and
  205. (Emacs 19 only) `Info-directory-list'.  Add the directory to the front
  206. of the list so the old "CL" package and its documentation are hidden.
  207.  
  208. 
  209. File: cl,  Node: Naming Conventions,  Prev: Installation,  Up: Overview
  210.  
  211. Naming Conventions
  212. ==================
  213.  
  214. Except where noted, all functions defined by this package have the same
  215. names and calling conventions as their Common Lisp counterparts.
  216.  
  217.    Following is a complete list of functions whose names were changed
  218. from Common Lisp, usually to avoid conflicts with Emacs.  In each case,
  219. a `*' has been appended to the Common Lisp name to obtain the Emacs
  220. name:
  221.  
  222.      defun*        defsubst*     defmacro*     function*
  223.      member*       assoc*        rassoc*       get*
  224.      remove*       delete*       mapcar*       sort*
  225.      floor*        ceiling*      truncate*     round*
  226.      mod*          rem*          random*
  227.  
  228.    Internal function and variable names in the package are prefixed by
  229. `cl-'.  Here is a complete list of functions *not* prefixed by `cl-'
  230. which were not taken from Common Lisp:
  231.  
  232.      member        delete        remove        remq
  233.      rassoc        floatp-safe   lexical-let   lexical-let*
  234.      callf         callf2        letf          letf*
  235.      defsubst*     defalias      add-hook      eval-when-compile
  236.  
  237. (Most of these are Emacs 19 features provided to Emacs 18 users, or
  238. introduced, like `remq', for reasons of symmetry with similar features.)
  239.  
  240.    The following simple functions and macros are defined in `cl.el';
  241. they do not cause other components like `cl-extra' to be loaded.
  242.  
  243.      eql           floatp-safe   abs           endp
  244.      evenp         oddp          plusp         minusp
  245.      last          butlast       nbutlast      caar .. cddddr
  246.      list*         ldiff         rest          first .. tenth
  247.      member [1]    copy-list     subst         mapcar* [2]
  248.      adjoin [3]    acons         pairlis       when
  249.      unless        pop [4]       push [4]      pushnew [3,4]
  250.      incf [4]      decf [4]      proclaim      declaim
  251.      add-hook
  252.  
  253. [1] This is the Emacs 19-compatible function, not `member*'.
  254.  
  255. [2] Only for one sequence argument or two list arguments.
  256.  
  257. [3] Only if `:test' is `eq', `equal', or unspecified, and `:key' is not
  258. used.
  259.  
  260. [4] Only when PLACE is a plain variable name.
  261.  
  262. 
  263. File: cl,  Node: Program Structure,  Next: Predicates,  Prev: Overview,  Up: Top
  264.  
  265. Program Structure
  266. *****************
  267.  
  268. This section describes features of the "CL" package which have to do
  269. with programs as a whole: advanced argument lists for functions, and
  270. the `eval-when' construct.
  271.  
  272. * Menu:
  273.  
  274. * Argument Lists::       `&key', `&aux', `defun*', `defmacro*'.
  275. * Time of Evaluation::   The `eval-when' construct.
  276. * Function Aliases::     The `defalias' function.
  277.  
  278. 
  279. File: cl,  Node: Argument Lists,  Next: Time of Evaluation,  Prev: Program Structure,  Up: Program Structure
  280.  
  281. Argument Lists
  282. ==============
  283.  
  284. Emacs Lisp's notation for argument lists of functions is a subset of
  285. the Common Lisp notation.  As well as the familiar `&optional' and
  286. `&rest' markers, Common Lisp allows you to specify default values for
  287. optional arguments, and it provides the additional markers `&key' and
  288. `&aux'.
  289.  
  290.    Since argument parsing is built-in to Emacs, there is no way for
  291. this package to implement Common Lisp argument lists seamlessly.
  292. Instead, this package defines alternates for several Lisp forms which
  293. you must use if you need Common Lisp argument lists.
  294.  
  295.  - Special Form: defun* NAME ARGLIST BODY...
  296.      This form is identical to the regular `defun' form, except that
  297.      ARGLIST is allowed to be a full Common Lisp argument list.  Also,
  298.      the function body is enclosed in an implicit block called NAME;
  299.      *note Blocks and Exits::..
  300.  
  301.  - Special Form: defsubst* NAME ARGLIST BODY...
  302.      This is just like `defun*', except that the function that is
  303.      defined is automatically proclaimed `inline', i.e., calls to it
  304.      may be expanded into in-line code by the byte compiler.  This is
  305.      analogous to the `defsubst' form in Emacs 19; `defsubst*' uses a
  306.      different method (compiler macros) which works in all version of
  307.      Emacs, and also generates somewhat more efficient inline
  308.      expansions.  In particular, `defsubst*' arranges for the
  309.      processing of keyword arguments, default values, etc., to be done
  310.      at compile-time whenever possible.
  311.  
  312.  - Special Form: defmacro* NAME ARGLIST BODY...
  313.      This is identical to the regular `defmacro' form, except that
  314.      ARGLIST is allowed to be a full Common Lisp argument list.  The
  315.      `&environment' keyword is supported as described in Steele.  The
  316.      `&whole' keyword is supported only within destructured lists (see
  317.      below); top-level `&whole' cannot be implemented with the current
  318.      Emacs Lisp interpreter.  The macro expander body is enclosed in an
  319.      implicit block called NAME.
  320.  
  321.  - Special Form: function* SYMBOL-OR-LAMBDA
  322.      This is identical to the regular `function' form, except that if
  323.      the argument is a `lambda' form then that form may use a full
  324.      Common Lisp argument list.
  325.  
  326.    Also, all forms (such as `defsetf' and `flet') defined in this
  327. package that include ARGLISTs in their syntax allow full Common Lisp
  328. argument lists.
  329.  
  330.    Note that it is *not* necessary to use `defun*' in order to have
  331. access to most "CL" features in your function.  These features are
  332. always present; `defun*''s only difference from `defun' is its more
  333. flexible argument lists and its implicit block.
  334.  
  335.    The full form of a Common Lisp argument list is
  336.  
  337.      (VAR...
  338.       &optional (VAR INITFORM SVAR)...
  339.       &rest VAR
  340.       &key ((KEYWORD VAR) INITFORM SVAR)...
  341.       &aux (VAR INITFORM)...)
  342.  
  343.    Each of the five argument list sections is optional.  The SVAR,
  344. INITFORM, and KEYWORD parts are optional; if they are omitted, then
  345. `(VAR)' may be written simply `VAR'.
  346.  
  347.    The first section consists of zero or more "required" arguments.
  348. These arguments must always be specified in a call to the function;
  349. there is no difference between Emacs Lisp and Common Lisp as far as
  350. required arguments are concerned.
  351.  
  352.    The second section consists of "optional" arguments.  These
  353. arguments may be specified in the function call; if they are not,
  354. INITFORM specifies the default value used for the argument.  (No
  355. INITFORM means to use `nil' as the default.)  The INITFORM is evaluated
  356. with the bindings for the preceding arguments already established; `(a
  357. &optional (b (1+ a)))' matches one or two arguments, with the second
  358. argument defaulting to one plus the first argument.  If the SVAR is
  359. specified, it is an auxiliary variable which is bound to `t' if the
  360. optional argument was specified, or to `nil' if the argument was
  361. omitted.  If you don't use an SVAR, then there will be no way for your
  362. function to tell whether it was called with no argument, or with the
  363. default value passed explicitly as an argument.
  364.  
  365.    The third section consists of a single "rest" argument.  If more
  366. arguments were passed to the function than are accounted for by the
  367. required and optional arguments, those extra arguments are collected
  368. into a list and bound to the "rest" argument variable.  Common Lisp's
  369. `&rest' is equivalent to that of Emacs Lisp.  Common Lisp accepts
  370. `&body' as a synonym for `&rest' in macro contexts; this package
  371. accepts it all the time.
  372.  
  373.    The fourth section consists of "keyword" arguments.  These are
  374. optional arguments which are specified by name rather than positionally
  375. in the argument list.  For example,
  376.  
  377.      (defun* foo (a &optional b &key c d (e 17)))
  378.  
  379. defines a function which may be called with one, two, or more
  380. arguments.  The first two arguments are bound to `a' and `b' in the
  381. usual way.  The remaining arguments must be pairs of the form `:c',
  382. `:d', or `:e' followed by the value to be bound to the corresponding
  383. argument variable.  (Symbols whose names begin with a colon are called
  384. "keywords", and they are self-quoting in the same way as `nil' and `t'.)
  385.  
  386.    For example, the call `(foo 1 2 :d 3 :c 4)' sets the five arguments
  387. to 1, 2, 4, 3, and 17, respectively.  If the same keyword appears more
  388. than once in the function call, the first occurrence takes precedence
  389. over the later ones.  Note that it is not possible to specify keyword
  390. arguments without specifying the optional argument `b' as well, since
  391. `(foo 1 :c 2)' would bind `b' to the keyword `:c', then signal an error
  392. because `2' is not a valid keyword.
  393.  
  394.    If a KEYWORD symbol is explicitly specified in the argument list as
  395. shown in the above diagram, then that keyword will be used instead of
  396. just the variable name prefixed with a colon.  You can specify a
  397. KEYWORD symbol which does not begin with a colon at all, but such
  398. symbols will not be self-quoting; you will have to quote them
  399. explicitly with an apostrophe in the function call.
  400.  
  401.    Ordinarily it is an error to pass an unrecognized keyword to a
  402. function, e.g., `(foo 1 2 :c 3 :goober 4)'.  You can ask Lisp to ignore
  403. unrecognized keywords, either by adding the marker `&allow-other-keys'
  404. after the keyword section of the argument list, or by specifying an
  405. `:allow-other-keys' argument in the call whose value is non-`nil'.  If
  406. the function uses both `&rest' and `&key' at the same time, the "rest"
  407. argument is bound to the keyword list as it appears in the call.  For
  408. example:
  409.  
  410.      (defun* find-thing (thing &rest rest &key need &allow-other-keys)
  411.        (or (apply 'member* thing thing-list :allow-other-keys t rest)
  412.            (if need (error "Thing not found"))))
  413.  
  414. This function takes a `:need' keyword argument, but also accepts other
  415. keyword arguments which are passed on to the `member*' function.
  416. `allow-other-keys' is used to keep both `find-thing' and `member*' from
  417. complaining about each others' keywords in the arguments.
  418.  
  419.    In Common Lisp, keywords are recognized by the Lisp parser itself
  420. and treated as special entities.  In Emacs, keywords are just symbols
  421. whose names begin with colons, which `defun*' has arranged to set equal
  422. to themselves so that they will essentially be self-quoting.
  423.  
  424.    As a (significant) performance optimization, this package implements
  425. the scan for keyword arguments by calling `memq' to search for keywords
  426. in a "rest" argument.  Technically speaking, this is incorrect, since
  427. `memq' looks at the odd-numbered values as well as the even-numbered
  428. keywords.  The net effect is that if you happen to pass a keyword symbol
  429. as the *value* of another keyword argument, where that keyword symbol
  430. happens to equal the name of a valid keyword argument of the same
  431. function, then the keyword parser will become confused.  This minor bug
  432. can only affect you if you use keyword symbols as general-purpose data
  433. in your program; this practice is strongly discouraged in Emacs Lisp.
  434.  
  435.    The fifth section of the argument list consists of "auxiliary
  436. variables".  These are not really arguments at all, but simply
  437. variables which are bound to `nil' or to the specified INITFORMS during
  438. execution of the function.  There is no difference between the
  439. following two functions, except for a matter of stylistic taste:
  440.  
  441.      (defun* foo (a b &aux (c (+ a b)) d)
  442.        BODY)
  443.      
  444.      (defun* foo (a b)
  445.        (let ((c (+ a b)) d)
  446.          BODY))
  447.  
  448.    Argument lists support "destructuring".  In Common Lisp,
  449. destructuring is only allowed with `defmacro'; this package allows it
  450. with `defun*' and other argument lists as well.  In destructuring, any
  451. argument variable (VAR in the above diagram) can be replaced by a list
  452. of variables, or more generally, a recursive argument list.  The
  453. corresponding argument value must be a list whose elements match this
  454. recursive argument list.  For example:
  455.  
  456.      (defmacro* dolist ((var listform &optional resultform)
  457.                         &rest body)
  458.        ...)
  459.  
  460.    This says that the first argument of `dolist' must be a list of two
  461. or three items; if there are other arguments as well as this list, they
  462. are stored in `body'.  All features allowed in regular argument lists
  463. are allowed in these recursive argument lists.  In addition, the clause
  464. `&whole VAR' is allowed at the front of a recursive argument list.  It
  465. binds VAR to the whole list being matched; thus `(&whole all a b)'
  466. matches a list of two things, with `a' bound to the first thing, `b'
  467. bound to the second thing, and `all' bound to the list itself.  (Common
  468. Lisp allows `&whole' in top-level `defmacro' argument lists as well,
  469. but Emacs Lisp does not support this usage.)
  470.  
  471.    One last feature of destructuring is that the argument list may be
  472. dotted, so that the argument list `(a b . c)' is functionally
  473. equivalent to `(a b &rest c)'.
  474.  
  475.    If the optimization quality `safety' is set to 0 (*note
  476. Declarations::.), error checking for wrong number of arguments and
  477. invalid keyword arguments is disabled.  By default, argument lists are
  478. rigorously checked.
  479.  
  480. 
  481. File: cl,  Node: Time of Evaluation,  Next: Function Aliases,  Prev: Argument Lists,  Up: Program Structure
  482.  
  483. Time of Evaluation
  484. ==================
  485.  
  486. Normally, the byte-compiler does not actually execute the forms in a
  487. file it compiles.  For example, if a file contains `(setq foo t)', the
  488. act of compiling it will not actually set `foo' to `t'.  This is true
  489. even if the `setq' was a top-level form (i.e., not enclosed in a
  490. `defun' or other form).  Sometimes, though, you would like to have
  491. certain top-level forms evaluated at compile-time.  For example, the
  492. compiler effectively evaluates `defmacro' forms at compile-time so that
  493. later parts of the file can refer to the macros that are defined.
  494.  
  495.  - Special Form: eval-when (SITUATIONS...) FORMS...
  496.      This form controls when the body FORMS are evaluated.  The
  497.      SITUATIONS list may contain any set of the symbols `compile',
  498.      `load', and `eval' (or their long-winded ANSI equivalents,
  499.      `:compile-toplevel', `:load-toplevel', and `:execute').
  500.  
  501.      The `eval-when' form is handled differently depending on whether
  502.      or not it is being compiled as a top-level form.  Specifically, it
  503.      gets special treatment if it is being compiled by a command such
  504.      as `byte-compile-file' which compiles files or buffers of code,
  505.      and it appears either literally at the top level of the file or
  506.      inside a top-level `progn'.
  507.  
  508.      For compiled top-level `eval-when's, the body FORMS are executed
  509.      at compile-time if `compile' is in the SITUATIONS list, and the
  510.      FORMS are written out to the file (to be executed at load-time) if
  511.      `load' is in the SITUATIONS list.
  512.  
  513.      For non-compiled-top-level forms, only the `eval' situation is
  514.      relevant.  (This includes forms executed by the interpreter, forms
  515.      compiled with `byte-compile' rather than `byte-compile-file', and
  516.      non-top-level forms.)  The `eval-when' acts like a `progn' if
  517.      `eval' is specified, and like `nil' (ignoring the body FORMS) if
  518.      not.
  519.  
  520.      The rules become more subtle when `eval-when's are nested; consult
  521.      Steele (second edition) for the gruesome details (and some
  522.      gruesome examples).
  523.  
  524.      Some simple examples:
  525.  
  526.           ;; Top-level forms in foo.el:
  527.           (eval-when (compile)           (setq foo1 'bar))
  528.           (eval-when (load)              (setq foo2 'bar))
  529.           (eval-when (compile load)      (setq foo3 'bar))
  530.           (eval-when (eval)              (setq foo4 'bar))
  531.           (eval-when (eval compile)      (setq foo5 'bar))
  532.           (eval-when (eval load)         (setq foo6 'bar))
  533.           (eval-when (eval compile load) (setq foo7 'bar))
  534.  
  535.      When `foo.el' is compiled, these variables will be set during the
  536.      compilation itself:
  537.  
  538.           foo1  foo3  foo5  foo7      ; `compile'
  539.  
  540.      When `foo.elc' is loaded, these variables will be set:
  541.  
  542.           foo2  foo3  foo6  foo7      ; `load'
  543.  
  544.      And if `foo.el' is loaded uncompiled, these variables will be set:
  545.  
  546.           foo4  foo5  foo6  foo7      ; `eval'
  547.  
  548.      If these seven `eval-when's had been, say, inside a `defun', then
  549.      the first three would have been equivalent to `nil' and the last
  550.      four would have been equivalent to the corresponding `setq's.
  551.  
  552.      Note that `(eval-when (load eval) ...)' is equivalent to `(progn
  553.      ...)' in all contexts.  The compiler treats certain top-level
  554.      forms, like `defmacro' (sort-of) and `require', as if they were
  555.      wrapped in `(eval-when (compile load eval) ...)'.
  556.  
  557.    Emacs 19 includes two special forms related to `eval-when'.  One of
  558. these, `eval-when-compile', is not quite equivalent to any `eval-when'
  559. construct and is described below.  This package defines a version of
  560. `eval-when-compile' for the benefit of Emacs 18 users.
  561.  
  562.    The other form, `(eval-and-compile ...)', is exactly equivalent to
  563. `(eval-when (compile load eval) ...)' and so is not itself defined by
  564. this package.
  565.  
  566.  - Special Form: eval-when-compile FORMS...
  567.      The FORMS are evaluated at compile-time; at execution time, this
  568.      form acts like a quoted constant of the resulting value.  Used at
  569.      top-level, `eval-when-compile' is just like `eval-when (compile
  570.      eval)'.  In other contexts, `eval-when-compile' allows code to be
  571.      evaluated once at compile-time for efficiency or other reasons.
  572.  
  573.      This form is similar to the `#.' syntax of true Common Lisp.
  574.  
  575.  - Special Form: load-time-value FORM
  576.      The FORM is evaluated at load-time; at execution time, this form
  577.      acts like a quoted constant of the resulting value.
  578.  
  579.      Early Common Lisp had a `#,' syntax that was similar to this, but
  580.      ANSI Common Lisp replaced it with `load-time-value' and gave it
  581.      more well-defined semantics.
  582.  
  583.      In a compiled file, `load-time-value' arranges for FORM to be
  584.      evaluated when the `.elc' file is loaded and then used as if it
  585.      were a quoted constant.  In code compiled by `byte-compile' rather
  586.      than `byte-compile-file', the effect is identical to
  587.      `eval-when-compile'.  In uncompiled code, both `eval-when-compile'
  588.      and `load-time-value' act exactly like `progn'.
  589.  
  590.           (defun report ()
  591.             (insert "This function was executed on: "
  592.                     (current-time-string)
  593.                     ", compiled on: "
  594.                     (eval-when-compile (current-time-string))
  595.                     ;; or '#.(current-time-string) in real Common Lisp
  596.                     ", and loaded on: "
  597.                     (load-time-value (current-time-string))))
  598.  
  599.      Byte-compiled, the above defun will result in the following code
  600.      (or its compiled equivalent, of course) in the `.elc' file:
  601.  
  602.           (setq --temp-- (current-time-string))
  603.           (defun report ()
  604.             (insert "This function was executed on: "
  605.                     (current-time-string)
  606.                     ", compiled on: "
  607.                     '"Wed Jun 23 18:33:43 1993"
  608.                     ", and loaded on: "
  609.                     --temp--))
  610.  
  611. 
  612. File: cl,  Node: Function Aliases,  Prev: Time of Evaluation,  Up: Program Structure
  613.  
  614. Function Aliases
  615. ================
  616.  
  617. This section describes a feature from GNU Emacs 19 which this package
  618. makes available in other versions of Emacs.
  619.  
  620.  - Function: defalias SYMBOL FUNCTION
  621.      This function sets SYMBOL's function cell to FUNCTION.  It is
  622.      equivalent to `fset', except that in GNU Emacs 19 it also records
  623.      the setting in `load-history' so that it can be undone by a later
  624.      `unload-feature'.
  625.  
  626.      In other versions of Emacs, `defalias' is a synonym for `fset'.
  627.  
  628. 
  629. File: cl,  Node: Predicates,  Next: Control Structure,  Prev: Program Structure,  Up: Top
  630.  
  631. Predicates
  632. **********
  633.  
  634. This section describes functions for testing whether various facts are
  635. true or false.
  636.  
  637. * Menu:
  638.  
  639. * Type Predicates::      `typep', `deftype', and `coerce'
  640. * Equality Predicates::  `eql' and `equalp'
  641.  
  642. 
  643. File: cl,  Node: Type Predicates,  Next: Equality Predicates,  Prev: Predicates,  Up: Predicates
  644.  
  645. Type Predicates
  646. ===============
  647.  
  648. The "CL" package defines a version of the Common Lisp `typep' predicate.
  649.  
  650.  - Function: typep OBJECT TYPE
  651.      Check if OBJECT is of type TYPE, where TYPE is a (quoted) type
  652.      name of the sort used by Common Lisp.  For example, `(typep foo
  653.      'integer)' is equivalent to `(integerp foo)'.
  654.  
  655.    The TYPE argument to the above function is either a symbol or a list
  656. beginning with a symbol.
  657.  
  658.    * If the type name is a symbol, Emacs appends `-p' to the symbol
  659.      name to form the name of a predicate function for testing the
  660.      type.  (Built-in predicates whose names end in `p' rather than
  661.      `-p' are used when appropriate.)
  662.  
  663.    * The type symbol `t' stands for the union of all types.  `(typep
  664.      OBJECT t)' is always true.  Likewise, the type symbol `nil' stands
  665.      for nothing at all, and `(typep OBJECT nil)' is always false.
  666.  
  667.    * The type symbol `null' represents the symbol `nil'.  Thus `(typep
  668.      OBJECT 'null)' is equivalent to `(null OBJECT)'.
  669.  
  670.    * The type symbol `real' is a synonym for `number', and `fixnum' is
  671.      a synonym for `integer'.
  672.  
  673.    * The type symbols `character' and `string-char' match integers in
  674.      the range from 0 to 255.
  675.  
  676.    * The type symbol `float' uses the `floatp-safe' predicate defined
  677.      by this package rather than `floatp', so it will work correctly
  678.      even in Emacs versions without floating-point support.
  679.  
  680.    * The type list `(integer LOW HIGH)' represents all integers between
  681.      LOW and HIGH, inclusive.  Either bound may be a list of a single
  682.      integer to specify an exclusive limit, or a `*' to specify no
  683.      limit.  The type `(integer * *)' is thus equivalent to `integer'.
  684.  
  685.    * Likewise, lists beginning with `float', `real', or `number'
  686.      represent numbers of that type falling in a particular range.
  687.  
  688.    * Lists beginning with `and', `or', and `not' form combinations of
  689.      types.  For example, `(or integer (float 0 *))' represents all
  690.      objects that are integers or non-negative floats.
  691.  
  692.    * Lists beginning with `member' or `member*' represent objects `eql'
  693.      to any of the following values.  For example, `(member 1 2 3 4)'
  694.      is equivalent to `(integer 1 4)', and `(member nil)' is equivalent
  695.      to `null'.
  696.  
  697.    * Lists of the form `(satisfies PREDICATE)' represent all objects
  698.      for which PREDICATE returns true when called with that object as
  699.      an argument.
  700.  
  701.    The following function and macro (not technically predicates) are
  702. related to `typep'.
  703.  
  704.  - Function: coerce OBJECT TYPE
  705.      This function attempts to convert OBJECT to the specified TYPE.
  706.      If OBJECT is already of that type as determined by `typep', it is
  707.      simply returned.  Otherwise, certain types of conversions will be
  708.      made:  If TYPE is any sequence type (`string', `list', etc.) then
  709.      OBJECT will be converted to that type if possible.  If TYPE is
  710.      `character', then strings of length one and symbols with
  711.      one-character names can be coerced.  If TYPE is `float', then
  712.      integers can be coerced in versions of Emacs that support floats.
  713.      In all other circumstances, `coerce' signals an error.
  714.  
  715.  - Special Form: deftype NAME ARGLIST FORMS...
  716.      This macro defines a new type called NAME.  It is similar to
  717.      `defmacro' in many ways; when NAME is encountered as a type name,
  718.      the body FORMS are evaluated and should return a type specifier
  719.      that is equivalent to the type.  The ARGLIST is a Common Lisp
  720.      argument list of the sort accepted by `defmacro*'.  The type
  721.      specifier `(NAME ARGS...)' is expanded by calling the expander
  722.      with those arguments; the type symbol `NAME' is expanded by
  723.      calling the expander with no arguments.  The ARGLIST is processed
  724.      the same as for `defmacro*' except that optional arguments without
  725.      explicit defaults use `*' instead of `nil' as the "default"
  726.      default.  Some examples:
  727.  
  728.           (deftype null () '(satisfies null))    ; predefined
  729.           (deftype list () '(or null cons))      ; predefined
  730.           (deftype unsigned-byte (&optional bits)
  731.             (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits)))))
  732.           (unsigned-byte 8)  ==  (integer 0 255)
  733.           (unsigned-byte)  ==  (integer 0 *)
  734.           unsigned-byte  ==  (integer 0 *)
  735.  
  736.      The last example shows how the Common Lisp `unsigned-byte' type
  737.      specifier could be implemented if desired; this package does not
  738.      implement `unsigned-byte' by default.
  739.  
  740.    The `typecase' and `check-type' macros also use type names.  *Note
  741. Conditionals::.  *Note Assertions::.  The `map', `concatenate', and
  742. `merge' functions take type-name arguments to specify the type of
  743. sequence to return.  *Note Sequences::.
  744.  
  745. 
  746. File: cl,  Node: Equality Predicates,  Prev: Type Predicates,  Up: Predicates
  747.  
  748. Equality Predicates
  749. ===================
  750.  
  751. This package defines two Common Lisp predicates, `eql' and `equalp'.
  752.  
  753.  - Function: eql A B
  754.      This function is almost the same as `eq', except that if A and B
  755.      are numbers of the same type, it compares them for numeric
  756.      equality (as if by `equal' instead of `eq').  This makes a
  757.      difference only for versions of Emacs that are compiled with
  758.      floating-point support, such as Emacs 19.  Emacs floats are
  759.      allocated objects just like cons cells, which means that `(eq 3.0
  760.      3.0)' will not necessarily be true--if the two `3.0's were
  761.      allocated separately, the pointers will be different even though
  762.      the numbers are the same.  But `(eql 3.0 3.0)' will always be true.
  763.  
  764.      The types of the arguments must match, so `(eql 3 3.0)' is still
  765.      false.
  766.  
  767.      Note that Emacs integers are "direct" rather than allocated, which
  768.      basically means `(eq 3 3)' will always be true.  Thus `eq' and
  769.      `eql' behave differently only if floating-point numbers are
  770.      involved, and are indistinguishable on Emacs versions that don't
  771.      support floats.
  772.  
  773.      There is a slight inconsistency with Common Lisp in the treatment
  774.      of positive and negative zeros.  Some machines, notably those with
  775.      IEEE standard arithmetic, represent `+0' and `-0' as distinct
  776.      values.  Normally this doesn't matter because the standard
  777.      specifies that `(= 0.0 -0.0)' should always be true, and this is
  778.      indeed what Emacs Lisp and Common Lisp do.  But the Common Lisp
  779.      standard states that `(eql 0.0 -0.0)' and `(equal 0.0 -0.0)' should
  780.      be false on IEEE-like machines; Emacs Lisp does not do this, and in
  781.      fact the only known way to distinguish between the two zeros in
  782.      Emacs Lisp is to `format' them and check for a minus sign.
  783.  
  784.  - Function: equalp A B
  785.      This function is a more flexible version of `equal'.  In
  786.      particular, it compares strings case-insensitively, and it compares
  787.      numbers without regard to type (so that `(equalp 3 3.0)' is true).
  788.      Vectors and conses are compared recursively.  All other objects
  789.      are compared as if by `equal'.
  790.  
  791.      This function differs from Common Lisp `equalp' in several
  792.      respects.  First, Common Lisp's `equalp' also compares
  793.      *characters* case-insensitively, which would be impractical in
  794.      this package since Emacs does not distinguish between integers and
  795.      characters.  In keeping with the idea that strings are less
  796.      vector-like in Emacs Lisp, this package's `equalp' also will not
  797.      compare strings against vectors of integers.  Finally, Common
  798.      Lisp's `equalp' compares hash tables without regard to ordering,
  799.      whereas this package simply compares hash tables in terms of their
  800.      underlying structure (which means vectors for Lucid Emacs 19 hash
  801.      tables, or lists for other hash tables).
  802.  
  803.    Also note that the Common Lisp functions `member' and `assoc' use
  804. `eql' to compare elements, whereas Emacs Lisp follows the MacLisp
  805. tradition and uses `equal' for these two functions.  In Emacs, use
  806. `member*' and `assoc*' to get functions which use `eql' for comparisons.
  807.  
  808. 
  809. File: cl,  Node: Control Structure,  Next: Macros,  Prev: Predicates,  Up: Top
  810.  
  811. Control Structure
  812. *****************
  813.  
  814. The features described in the following sections implement various
  815. advanced control structures, including the powerful `setf' facility and
  816. a number of looping and conditional constructs.
  817.  
  818. * Menu:
  819.  
  820. * Assignment::             The `psetq' form
  821. * Generalized Variables::  `setf', `incf', `push', etc.
  822. * Variable Bindings::      `progv', `lexical-let', `flet', `macrolet'
  823. * Conditionals::           `when', `unless', `case', `typecase'
  824. * Blocks and Exits::       `block', `return', `return-from'
  825. * Iteration::              `do', `dotimes', `dolist', `do-symbols'
  826. * Loop Facility::          The Common Lisp `loop' macro
  827. * Multiple Values::        `values', `multiple-value-bind', etc.
  828.  
  829. 
  830. File: cl,  Node: Assignment,  Next: Generalized Variables,  Prev: Control Structure,  Up: Control Structure
  831.  
  832. Assignment
  833. ==========
  834.  
  835. The `psetq' form is just like `setq', except that multiple assignments
  836. are done in parallel rather than sequentially.
  837.  
  838.  - Special Form: psetq [SYMBOL FORM]...
  839.      This special form (actually a macro) is used to assign to several
  840.      variables simultaneously.  Given only one SYMBOL and FORM, it has
  841.      the same effect as `setq'.  Given several SYMBOL and FORM pairs,
  842.      it evaluates all the FORMs in advance and then stores the
  843.      corresponding variables afterwards.
  844.  
  845.           (setq x 2 y 3)
  846.           (setq x (+ x y)  y (* x y))
  847.           x
  848.                => 5
  849.           y                     ; `y' was computed after `x' was set.
  850.                => 15
  851.           (setq x 2 y 3)
  852.           (psetq x (+ x y)  y (* x y))
  853.           x
  854.                => 5
  855.           y                     ; `y' was computed before `x' was set.
  856.                => 6
  857.  
  858.      The simplest use of `psetq' is `(psetq x y y x)', which exchanges
  859.      the values of two variables.  (The `rotatef' form provides an even
  860.      more convenient way to swap two variables; *note Modify Macros::..)
  861.  
  862.      `psetq' always returns `nil'.
  863.  
  864. 
  865. File: cl,  Node: Generalized Variables,  Next: Variable Bindings,  Prev: Assignment,  Up: Control Structure
  866.  
  867. Generalized Variables
  868. =====================
  869.  
  870. A "generalized variable" or "place form" is one of the many places in
  871. Lisp memory where values can be stored.  The simplest place form is a
  872. regular Lisp variable.  But the cars and cdrs of lists, elements of
  873. arrays, properties of symbols, and many other locations are also places
  874. where Lisp values are stored.
  875.  
  876.    The `setf' form is like `setq', except that it accepts arbitrary
  877. place forms on the left side rather than just symbols.  For example,
  878. `(setf (car a) b)' sets the car of `a' to `b', doing the same operation
  879. as `(setcar a b)' but without having to remember two separate functions
  880. for setting and accessing every type of place.
  881.  
  882.    Generalized variables are analogous to "lvalues" in the C language,
  883. where `x = a[i]' gets an element from an array and `a[i] = x' stores an
  884. element using the same notation.  Just as certain forms like `a[i]' can
  885. be lvalues in C, there is a set of forms that can be generalized
  886. variables in Lisp.
  887.  
  888. * Menu:
  889.  
  890. * Basic Setf::         `setf' and place forms
  891. * Modify Macros::      `incf', `push', `rotatef', `letf', `callf', etc.
  892. * Customizing Setf::   `define-modify-macro', `defsetf', `define-setf-method'
  893.  
  894. 
  895. File: cl,  Node: Basic Setf,  Next: Modify Macros,  Prev: Generalized Variables,  Up: Generalized Variables
  896.  
  897. Basic Setf
  898. ----------
  899.  
  900. The `setf' macro is the most basic way to operate on generalized
  901. variables.
  902.  
  903.  - Special Form: setf [PLACE FORM]...
  904.      This macro evaluates FORM and stores it in PLACE, which must be a
  905.      valid generalized variable form.  If there are several PLACE and
  906.      FORM pairs, the assignments are done sequentially just as with
  907.      `setq'.  `setf' returns the value of the last FORM.
  908.  
  909.      The following Lisp forms will work as generalized variables, and
  910.      so may legally appear in the PLACE argument of `setf':
  911.  
  912.         * A symbol naming a variable.  In other words, `(setf x y)' is
  913.           exactly equivalent to `(setq x y)', and `setq' itself is
  914.           strictly speaking redundant now that `setf' exists.  Many
  915.           programmers continue to prefer `setq' for setting simple
  916.           variables, though, purely for stylistic or historical reasons.
  917.           The macro `(setf x y)' actually expands to `(setq x y)', so
  918.           there is no performance penalty for using it in compiled code.
  919.  
  920.         * A call to any of the following Lisp functions:
  921.  
  922.                car                 cdr                 caar .. cddddr
  923.                nth                 rest                first .. tenth
  924.                aref                elt                 nthcdr
  925.                symbol-function     symbol-value        symbol-plist
  926.                get                 get*                getf
  927.                gethash             subseq
  928.  
  929.           Note that for `nthcdr' and `getf', the list argument of the
  930.           function must itself be a valid PLACE form.  For example,
  931.           `(setf (nthcdr 0 foo) 7)' will set `foo' itself to 7.  Note
  932.           that `push' and `pop' on an `nthcdr' place can be used to
  933.           insert or delete at any position in a list.  The use of
  934.           `nthcdr' as a PLACE form is an extension to standard Common
  935.           Lisp.
  936.  
  937.         * The following Emacs-specific functions are also `setf'-able.
  938.           (Some of these are defined only in Emacs 19 or only in Lucid
  939.           Emacs.)
  940.  
  941.                buffer-file-name                  marker-position
  942.                buffer-modified-p                 match-data
  943.                buffer-name                       mouse-position
  944.                buffer-string                     overlay-end
  945.                buffer-substring                  overlay-get
  946.                current-buffer                    overlay-start
  947.                current-case-table                point
  948.                current-column                    point-marker
  949.                current-global-map                point-max
  950.                current-input-mode                point-min
  951.                current-local-map                 process-buffer
  952.                current-window-configuration      process-filter
  953.                default-file-modes                process-sentinel
  954.                default-value                     read-mouse-position
  955.                documentation-property            screen-height
  956.                extent-data                       screen-menubar
  957.                extent-end-position               screen-width
  958.                extent-start-position             selected-window
  959.                face-background                   selected-screen
  960.                face-background-pixmap            selected-frame
  961.                face-font                         standard-case-table
  962.                face-foreground                   syntax-table
  963.                face-underline-p                  window-buffer
  964.                file-modes                        window-dedicated-p
  965.                frame-height                      window-display-table
  966.                frame-parameters                  window-height
  967.                frame-visible-p                   window-hscroll
  968.                frame-width                       window-point
  969.                get-register                      window-start
  970.                getenv                            window-width
  971.                global-key-binding                x-get-cut-buffer
  972.                keymap-parent                     x-get-cutbuffer
  973.                local-key-binding                 x-get-secondary-selection
  974.                mark                              x-get-selection
  975.                mark-marker
  976.  
  977.           Most of these have directly corresponding "set" functions,
  978.           like `use-local-map' for `current-local-map', or `goto-char'
  979.           for `point'.  A few, like `point-min', expand to longer
  980.           sequences of code when they are `setf''d (`(narrow-to-region
  981.           x (point-max))' in this case).
  982.  
  983.         * A call of the form `(substring SUBPLACE N [M])', where
  984.           SUBPLACE is itself a legal generalized variable whose current
  985.           value is a string, and where the value stored is also a
  986.           string.  The new string is spliced into the specified part of
  987.           the destination string.  For example:
  988.  
  989.                (setq a (list "hello" "world"))
  990.                     => ("hello" "world")
  991.                (cadr a)
  992.                     => "world"
  993.                (substring (cadr a) 2 4)
  994.                     => "rl"
  995.                (setf (substring (cadr a) 2 4) "o")
  996.                     => "o"
  997.                (cadr a)
  998.                     => "wood"
  999.                a
  1000.                     => ("hello" "wood")
  1001.  
  1002.           The generalized variable `buffer-substring', listed above,
  1003.           also works in this way by replacing a portion of the current
  1004.           buffer.
  1005.  
  1006.         * A call of the form `(apply 'FUNC ...)' or `(apply (function
  1007.           FUNC) ...)', where FUNC is a `setf'-able function whose store
  1008.           function is "suitable" in the sense described in Steele's
  1009.           book; since none of the standard Emacs place functions are
  1010.           suitable in this sense, this feature is only interesting when
  1011.           used with places you define yourself with
  1012.           `define-setf-method' or the long form of `defsetf'.
  1013.  
  1014.         * A macro call, in which case the macro is expanded and `setf'
  1015.           is applied to the resulting form.
  1016.  
  1017.         * Any form for which a `defsetf' or `define-setf-method' has
  1018.           been made.
  1019.  
  1020.      Using any forms other than these in the PLACE argument to `setf'
  1021.      will signal an error.
  1022.  
  1023.      The `setf' macro takes care to evaluate all subforms in the proper
  1024.      left-to-right order; for example,
  1025.  
  1026.           (setf (aref vec (incf i)) i)
  1027.  
  1028.      looks like it will evaluate `(incf i)' exactly once, before the
  1029.      following access to `i'; the `setf' expander will insert temporary
  1030.      variables as necessary to ensure that it does in fact work this
  1031.      way no matter what setf-method is defined for `aref'.  (In this
  1032.      case, `aset' would be used and no such steps would be necessary
  1033.      since `aset' takes its arguments in a convenient order.)
  1034.  
  1035.      However, if the PLACE form is a macro which explicitly evaluates
  1036.      its arguments in an unusual order, this unusual order will be
  1037.      preserved.  Adapting an example from Steele, given
  1038.  
  1039.           (defmacro wrong-order (x y) (list 'aref y x))
  1040.  
  1041.      the form `(setf (wrong-order A B) 17)' will evaluate B first, then
  1042.      A, just as in an actual call to `wrong-order'.
  1043.  
  1044.